<?php

namespace app\api\controller\permission;

use app\api\controller\common\ApiBase;
use app\api\model\Auth as AuthModel;

class Auth extends ApiBase
{

    /**
     * 权限列表
     *
     * @return void
     */
    public function index()
    {
        if (request()->isPost()) {
            $authList = AuthModel::field('id,auth_name,status,rule,parent_id,sort,create_time,update_time,level,path')->order('sort desc')->select();
            $authList = collection($authList)->toArray();
            foreach ($authList as $k => $v) {
                if ($v['level'] == 3) {
                    $p = AuthModel::get($v['parent_id']);
                    $authList[$k]['parent_id_arr'] = [$p['parent_id'], $v['parent_id']];
                }
                if ($v['level'] == 2) {
                    $authList[$k]['parent_id_arr'] = [$v['parent_id']];
                }
                if ($v['level'] == 1) {
                    $authList[$k]['parent_id_arr'] = [];
                }
            }
            $authList = list_to_tree($authList);
            return json(array('code' => '1', 'msg' => '成功', 'data' => $authList));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 新增权限
     *
     * @return void
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $auth = new AuthModel();
            $data['auth_name'] = isset($post['auth_name']) && $post['auth_name'] != '' ? $post['auth_name'] : '';
            $data['status'] = isset($post['status']) && $post['status'] != '' ? $post['status'] : 0;
            $data['rule'] = str_replace("，",",",str_replace("/",",",strtolower(isset($post['rule']) && $post['rule'] != '' ? $post['rule'] : '')));
            $data['parent_id'] = isset($post['parent_id']) && $post['parent_id'] != '' ? $post['parent_id'] : 0;
            $data['sort'] = isset($post['sort']) && $post['sort'] != '' ? $post['sort'] : 0;
            $data['path'] = isset($post['path']) && $post['path'] != '' ? $post['path'] : '';
            if ($data['parent_id'] == 0) {
                $data['level'] = 1;
            } else {
                $authInfo = AuthModel::get($data['parent_id']);
                $data['level'] = $authInfo['level'] + 1;
            }
            $result = $auth->validate('auth.add')->save($data);
            if (false === $result) {
                $msg = $auth->getError();
                return json(array('code' => '0', 'msg' => $msg));
            } else {
                return json(array('code' => '1', 'msg' => '成功'));
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 编辑权限
     *
     * @return void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = $post['id'];
            $auth = AuthModel::get($id);
            $data['auth_name'] = isset($post['auth_name']) && $post['auth_name'] != '' ? $post['auth_name'] : '';
            $data['status'] = isset($post['status']) && $post['status'] != '' ? $post['status'] : 0;
            $data['rule'] = str_replace("，",",",str_replace("/",",",strtolower(isset($post['rule']) && $post['rule'] != '' ? $post['rule'] : '')));
            $data['parent_id'] = isset($post['parent_id']) && $post['parent_id'] != '' ? $post['parent_id'] : 0;
            $data['sort'] = isset($post['sort']) && $post['sort'] != '' ? $post['sort'] : 0;
            $data['path'] = isset($post['path']) && $post['path'] != '' ? $post['path'] : '';
            if ($data['parent_id'] == 0) {
                $data['level'] = 1;
            } else {
                $authInfo = AuthModel::get($data['parent_id']);
                $data['level'] = $authInfo['level'] + 1;
            }
            $result = $auth->validate('auth.edit')->save($data, ['id' => $id]);
            if (false === $result) {
                $msg = $auth->getError();
                return json(array('code' => '0', 'msg' => $msg));
            } else {
                return json(array('code' => '1', 'msg' => '成功'));
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 删除权限
     *
     * @return void
     */
    public function del()
    {
        if (request()->isPost()) {
            $id = input('post.id');
            $children = AuthModel::where(['parent_id' => $id])->select();
            if (count($children) > 0) {
                return json(array('code' => '0', 'msg' => '该权限规则下有子权限规则，不能直接删除'));
            }
            $auth = AuthModel::get($id);
            $result = $auth->delete();
            if (false === $result) {
                $msg = $auth->getError();
                return json(array('code' => '0', 'msg' => $msg));
            } else {
                return json(array('code' => '1', 'msg' => '成功'));
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }
}
